<template>
    <div class="_edit">
        <page-main class="_main">
            <!-- 返回 -->
            <el-button
                type="primary"
                icon="el-icon-d-arrow-left"
                @click="_return"
            >
                返回
            </el-button>
            <!-- 用户昵称 -->
            <div>
                <span>用户昵称</span>
                <el-input v-model="nickName" type="text" />
            </div>

            <!-- 用户头像 -->
            <div>
                <span>用户头像</span>
                <el-upload
                    v-model="avatarUrl"
                    class="upload-demo"
                    drag
                    :limit="1"
                    :on-change="fileListfunc"
                    :file-list="fileList"
                    :auto-upload="false"
                    action=""
                    multiple
                    list-type="picture"
                >
                    <i class="el-icon-upload" />
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                </el-upload>
            </div>
            <!-- 累计积分 -->
            <div>
                <span>累计积分</span>
                <el-input v-model="allintegral" type="text" />
            </div>
            <!-- 当前积分 -->
            <div>
                <span>当前积分</span>
                <el-input v-model="integral" type="text" />
            </div>
            <!-- openid -->
            <div>
                <span>openid</span>
                <el-input v-model="openid" type="text" />
            </div>
            <!-- 最近登录时间 -->
            <div>
                <span>最近登录时间</span>
                <div>
                    <el-date-picker
                        v-model="logtime"
                        type="datetime"
                        placeholder="选择日期时间"
                        align="right"
                        :picker-options="pickerOptions"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="timestamp"
                    />
                </div>
            </div>
            <!-- 地址 -->
            <div>
                <span>地址</span>
                <el-input v-model="addressinfo" type="text" />
            </div>
            <!-- 审核状态 -->
            <div>
                <span>审核状态</span>
                <div>
                    <el-select v-model="ck" placeholder="请选择">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <!-- 邮箱 -->
            <div>
                <span>邮箱</span>
                <el-input v-model="email" type="text" />
            </div>
            <!-- 伙伴 -->
            <div>
                <span>伙伴</span>
                <el-input v-model="partners" type="text" />
            </div>
            <!-- 手机号 -->
            <div>
                <span>手机号</span>
                <el-input v-model="phone" type="text" />
            </div>
            <!-- 邮箱认证 -->
            <div>
                <span>邮箱认证</span>
                <div>
                    <el-select v-model="emailreg" placeholder="请选择">
                        <el-option
                            v-for="item in options2"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <!-- 取消与立即修改 -->
            <el-row class="operation">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="upd()">创建</el-button>
            </el-row>
        </page-main>
    </div>
</template>
<script>

export default {
    data() {
        return {
            avatarUrl: '',
            nickName: '',
            allintegral: '',
            integral: '',
            openid: '',
            addressinfo: '',
            email: '',
            partners: '',
            phone: '',
            firsttime: '',
            fileList: [],

            // 邮箱认证
            emailreg: '',

            options2: [{
                value: '1',
                label: '已认证'
            }, {
                value: '2',
                label: '未认证'
            }
            ],
            // 审核状态
            ck: '',
            options: [{
                value: '1',
                label: '未通过'
            }, {
                value: '2',
                label: '已通过'
            }, {
                value: '3',
                label: '已离职'
            }, {
                value: '4',
                label: '审核中'
            }],
            // 最近登录时间
            logtime: '',
            pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date())
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date()
                        date.setTime(date.getTime() - 3600 * 1000 * 24)
                        picker.$emit('pick', date)
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date()
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
                        picker.$emit('pick', date)
                    }
                }]
            }

        }
    },
    methods: {
        // 获取当前年月日时分秒
        mounted() {
            var that = this
            let yy = new Date().getFullYear()
            let mm = new Date().getMonth() + 1
            let dd = new Date().getDate()
            let hh = new Date().getHours()
            let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
            let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
            that.firsttime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
        },
        // 时间戳转换为年月日时分秒
        timestampToTime(value) {
            if (typeof (value) == 'undefined') {
                return ''
            } else {
                let date = new Date(parseInt(value))
                let y = date.getFullYear()
                let MM = date.getMonth() + 1
                MM = MM < 10 ? ('0' + MM) : MM
                let d = date.getDate()
                d = d < 10 ? ('0' + d) : d
                let h = date.getHours()
                h = h < 10 ? ('0' + h) : h
                let m = date.getMinutes()
                m = m < 10 ? ('0' + m) : m
                let s = date.getSeconds()
                s = s < 10 ? ('0' + s) : s
                return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
            }
        },
        // 返回
        _return() {
            this.$router.go(-1)
        },
        // 上传图片
        async fileListfunc(file, fileList) {

            console.log('file', file, 'fileList', fileList)
            this.file = fileList[0].raw
            var flx = this.file.type.split('/')[1]
            var fileda =  await this.$cloudbase
                .uploadFile({
                    cloudPath: 'user' + new Date().getTime() + '.' + flx,
                    filePath: this.file
                })
            this.avatarUrl = fileda.fileID

        },
        // 取消
        cancel() {
            this.$router.go(-1)
        },
        // 创建
        upd() {
            let that = this
            // 获取时间戳
            let timestamp = Date.parse(new Date())
            this.firsttime = timestamp

            var user = this.$cloudbase.database().collection('user')
            user.add({
                avatarUrl: that.avatarUrl,
                nickName: that.nickName,
                allintegral: that.allintegral,
                integral: that.integral,
                openid: that.openid,
                addressinfo: that.addressinfo,
                email: that.email,
                partners: that.partners,
                phone: that.phone,
                emailreg: that.emailreg,
                ck: that.ck,
                logtime: that.logtime,
                firsttime: that.firsttime

            }).then(() => {
                this.$router.go(-1)
            })
        }

    }
}
</script>

<style>
._edit ._main .el-button {
    margin-bottom: 20px;
}
._edit ._main div {
    margin-bottom: 10px;
}
._edit ._main div span {
    display: block;
    margin-bottom: 5px;
}
._edit ._main .operation span {
    margin-bottom: 0;
}
._edit ._main .fileId {
    margin-bottom: 20px;
}
._edit ._main .el-switch {
    margin-bottom: 20px;
}

</style>
